package sedev.struts.portlet;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import sedev.shopping.business.NhomSanPhamService;
import sedev.shopping.entities.ListNhomSanPham;
import sedev.shopping.entities.NhomSanPham;
import sedev.shopping.util.SedevUtil;
import sedev.shopping.util.SedevUtil.ModeDisplay;

import com.liferay.portal.util.PortalUtil;
import com.opensymphony.xwork2.ActionSupport;

public class ProductGroupAction extends ActionSupport implements
		ServletRequestAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private Long ownerId;
	private Boolean isAdmin;

	private int preExecute(){
		if(this.isAdmin == false){
			return -1;
		}
		return 0;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
		this.ownerId = PortalUtil.getUserId(this.request);
		this.isAdmin = SedevUtil.isAdministrator(this.ownerId);
	}

	@Override
	public String execute() throws Exception {
		if(this.preExecute() < 0){
			return "dinied";
		}
		String actionResult = "";
		ListNhomSanPham listnhomsanpham = new ListNhomSanPham();
		listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.All);
		this.request.setAttribute("listnhomsanpham", listnhomsanpham);
		actionResult = ActionSupport.SUCCESS;
		return actionResult;
	}

	public String post() throws Exception{		
		if(this.preExecute() < 0){
			return "denied";
		}
		ListNhomSanPham listnhomsanpham = new ListNhomSanPham();
		listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.All);
		this.request.setAttribute("listnhomsanpham", listnhomsanpham);
		return ActionSupport.SUCCESS;
	}
	
	public String edit() throws Exception{
		if(this.preExecute() < 0){
			return "denied";
		}
		NhomSanPham data = new NhomSanPham();
		try{
			Long id =Long.valueOf((String) this.request.getParameter("groupid"));
			data = NhomSanPhamService.getData(id);
			if(data!=null){
				this.request.setAttribute("nhomsanpham", data);
				ListNhomSanPham listnhomsanpham = new ListNhomSanPham();
				listnhomsanpham = NhomSanPhamService.getListDataByDiffId(data.getId(), ModeDisplay.All);
				this.request.setAttribute("listnhomsanpham", listnhomsanpham);
				return ActionSupport.SUCCESS;
			}else{
				this.request.setAttribute("errorCode", -1);
				ListNhomSanPham listnhomsanpham = new ListNhomSanPham();
				listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.All);
				this.request.setAttribute("listnhomsanpham", listnhomsanpham);
				return ActionSupport.ERROR;
			}
		}catch(Exception ex){
			this.request.setAttribute("errorCode", -1);
			ListNhomSanPham listnhomsanpham = new ListNhomSanPham();
			listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.All);
			this.request.setAttribute("listnhomsanpham", listnhomsanpham);
			return ActionSupport.ERROR;
		}
	}
	
	public String add() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		NhomSanPham data = new NhomSanPham();
		try {
			if (this.initData(data)) {
				if (NhomSanPhamService.addData(data)) {
					this.request.setAttribute("nhomsanpham", data);
					this.request.setAttribute("errorCode", 1);
					ListNhomSanPham listnhomsanpham = new ListNhomSanPham();
					listnhomsanpham = NhomSanPhamService.getListDataByDiffId(data.getId(), ModeDisplay.All);
					this.request.setAttribute("listnhomsanpham", listnhomsanpham);
					return ActionSupport.SUCCESS;
				}
			}
		} catch (Exception ex) {			
		}
		this.request.setAttribute("errorCode", -1);
		ListNhomSanPham listnhomsanpham = new ListNhomSanPham();
		listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.All);
		this.request.setAttribute("listnhomsanpham", listnhomsanpham);
		return ActionSupport.ERROR;
	}

	public String update() throws Exception {
		if(this.preExecute() < 0){
			return "denied";
		}
		NhomSanPham data = new NhomSanPham();
		try {
			data.setId(Long.valueOf(this.request.getParameter("id")));
			if (data.getId() != null) {				
				if (this.initData(data)) {
					if (NhomSanPhamService.updateData(data)) {
						this.request.setAttribute("errorCode", 2);						
					} else {
						this.request.setAttribute("errorCode", -1);
					}
				} else {
					this.request.setAttribute("errorCode", -1);
				}
				this.request.setAttribute("nhomsanpham", data);
				ListNhomSanPham listnhomsanpham = new ListNhomSanPham();
				listnhomsanpham = NhomSanPhamService.getListDataByDiffId(data.getId(), ModeDisplay.All);
				this.request.setAttribute("listnhomsanpham", listnhomsanpham);
				return ActionSupport.SUCCESS;
			}			
		} catch (Exception ex) {						
		}
		this.request.setAttribute("errorCode", -1);
		ListNhomSanPham listnhomsanpham = new ListNhomSanPham();
		listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.All);
		this.request.setAttribute("listnhomsanpham", listnhomsanpham);
		return ActionSupport.ERROR;
	}

	public String remove() throws Exception{
		if(this.preExecute() < 0){
			return "denied";
		}
		long id = Long.valueOf(this.request.getParameter("id"));
		if(NhomSanPhamService.removeData(id)){
			this.request.setAttribute("errorCode", 1);			
		}else{
			this.request.setAttribute("errorCode", -1);
		}
		ListNhomSanPham listnhomsanpham = NhomSanPhamService.getListDataByAll(ModeDisplay.All);
		this.request.setAttribute("listnhomsanpham", listnhomsanpham);
		return ActionSupport.SUCCESS;
	}
	
	private boolean initData(NhomSanPham data) {
		try {
			data.setTen(this.request.getParameter("title"));
		} catch (Exception ex) {
			data.setTen("");
		}
		try {
			data.setNhomSanPhamId(Long.valueOf(this.request.getParameter("groupparent")));
		} catch (Exception ex) {
			data.setNhomSanPhamId(Long.valueOf(0));
		}
		try {
			Integer isHienThi = Integer.valueOf(this.request.getParameter("published"));
			if(isHienThi > 0){
				data.setHienThi(true);
			}else{
				data.setHienThi(false);
			}			
		} catch (Exception ex) {
			data.setHienThi(false);
		}
		try {
			data.setThuocTinh(this.request.getParameter("attribute"));
		} catch (Exception ex) {
			data.setThuocTinh("");
		}		
		try {
			data.setNgayDang(SedevUtil.getTime());
		} catch (Exception ex) {
			data.setNgayDang(Long.valueOf(0));
		}
		return true;
	}

}
